﻿using PMS.Data.Entities.SystemModules;
using PMS.Services.EntityServices;

namespace PMS.Services.EventHandler.Caching
{
    /// <summary>
    /// Represents a SystemModule-CustomerRole Mapping cache event consumer
    /// </summary>
    public partial class SystemModuleCustomerRoleMappingCacheEventHandler : CacheEventHandler<SystemModuleCustomerRoleMapping>
    {
        /// <summary>
        /// 分别针对Insert/Update/Delete进行操作，
        /// 如果操作逻辑相同，可以调用 ClearCache(Setting entity)
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="entityEventType"></param>
        protected override void ClearCache(SystemModuleCustomerRoleMapping entity, EntityEventType entityEventType)
        {
            ClearCache(entity);
        }
        /// <summary>
        /// Clear cache data
        /// </summary>
        /// <param name="entity">Entity</param>
        protected override void ClearCache(SystemModuleCustomerRoleMapping entity)
        {
            var key = _cacheKeyService.PrepareKeyForDefaultCache(CacheKeyConfigs.SystemModulesByCustomerRoleCacheKey, entity.CustomerRoleId);
            Remove(key);
            key = _cacheKeyService.PrepareKeyForDefaultCache(CacheKeyConfigs.ModuleRoleMappingsByCustomerRoleCacheKey, entity.CustomerRoleId);
            Remove(key);
            if(entity.SystemModuleId > 0)
            {
                key = _cacheKeyService.PrepareKeyForDefaultCache(CacheKeyConfigs.ModuleRoleMappingsByModuleIdCacheKey, entity.SystemModuleId);
                Remove(key);
            }
            else RemoveByPrefix(CacheKeyConfigs.ModuleRoleMappingsAllPrefixCacheKey);
        }
    }

}
